//
//  TGMaskView.m
//  WanCaiC8
//
//  Created by QiuXun on 2019/1/18.
//  Copyright © 2019 tangang. All rights reserved.
//

#import "TGMaskCollectionView.h"
#import "LabCell.h"

#import "TGTrendListDetal_LogoModel.h"

@interface TGMaskCollectionView ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic,strong)UIView *bgView;

@property(nonatomic,strong)UIView *contentView;

@property(nonatomic,strong)UICollectionView *collectionView;
@end
@implementation TGMaskCollectionView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)setupUI{
    
    [self addSubview:self.contentView];
    
}

-(void)setDataArr:(NSArray *)dataArr{
    
    _dataArr = dataArr;
    
    [self.collectionView reloadData];
    
}



- (void)show{
    // 自定义设置统一在这边刷新一次
//    if (_needReload) [self reloadData];
    
    [[UIApplication sharedApplication].keyWindow addSubview: self.bgView];
    [[UIApplication sharedApplication].keyWindow addSubview: self];
    self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(1.0, 1.0);
        self.alpha = 1.0f;
        self.bgView.alpha = 1.0f;
    }];
}


-(void)dismiss{
//    if (!_dismissOnTouchOutside) return;
    [UIView animateWithDuration: 0.25 animations:^{
        self.layer.affineTransform = CGAffineTransformMakeScale(0.1, 0.1);
        self.alpha = 0.0f;
        self.bgView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        [self.bgView removeFromSuperview];
    }];
}


- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _bgView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
        _bgView.alpha = 0.0f;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismiss)];
        [_bgView addGestureRecognizer:tap];
    }
    return _bgView;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] initWithFrame:self.bounds];
        _contentView.backgroundColor = [UIColor whiteColor];
        _contentView.layer.masksToBounds = YES;
        [_contentView addSubview:self.collectionView];
    }
    return _contentView;
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
 
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        
        [layout setItemSize:CGSizeMake((SCREEN_WIDTH-25*3)/4.0, 30)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 5;
        layout.minimumLineSpacing = 5;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectInset(self.contentView.bounds, 5, 5) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator=NO;
        
        [_collectionView registerClass:[LabCell class] forCellWithReuseIdentifier:@"LabCell"];

//        [_collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.edges.equalTo(self.contentView).with.insets(UIEdgeInsetsMake(0, 0, 0, 50));
//
//        }];
    }
    
    return _collectionView;
}




    
#pragma mark -UICollectionViewDelegate,UICollectionViewDataSource

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
  
    return self.dataArr.count;
    
}




-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{

    LabCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LabCell" forIndexPath:indexPath];
  
    TGTrendListDetal_LogoModel *model = self.dataArr[indexPath.row];

    [cell setBGColorWithTitle:model.Name andIsSelect:NO];

    return cell;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    TGTrendListDetal_LogoModel *model = self.dataArr[indexPath.row];
    
    
    if (self.chaneBlock) {

        self.chaneBlock(model.Name);
        
        [self dismiss];

    }
    
}



@end
